package com.beardedcoder.utils.db.other;

public class DbField {
	private static final String SPACER = " ";
	private static final String PRIMARYKEY = "primary key";
	private static final String AUTOINCREMENT = "autoincrement";
	public String fieldType;
	public int fieldOrdinalPos;
	public String fieldName;
	public boolean autoIncrement = false;
	public boolean primaryKey = false;
	public boolean lastField;
	
	public DbField(Builder builder) {
		fieldType = builder.type;
		fieldName = builder.name;
		autoIncrement = builder.autoIncrement;
		primaryKey = builder.primaryKey;
		lastField = builder.lastField;
	}
	
	public static class Builder {
		// required fields
		private String name;
		private String type;
		
		// optional fields
		private boolean primaryKey = false;
		private boolean autoIncrement = false;
		private boolean lastField = false;
		
		// constructor
		public Builder (String name, String type) {
			this.name = name;
			this.type = type;
		}
		
		public Builder setPrimaryKey (boolean primary) {
			this.primaryKey = primary;
			return this;
		}
		
		public Builder setAutoIncrement (boolean auto) {
			this.autoIncrement = auto;
			return this;
		}
		
		public Builder setLastField (boolean lastField) {
			this.lastField = lastField;
			return this;
		}
		
		public DbField build() {
			return new DbField(this);
		}
	}
	
	public String getFieldNameAndType() {
		StringBuffer sb = new StringBuffer();
		
		sb.append(fieldName).append(SPACER);
		
		if (primaryKey) {
			sb.append(PRIMARYKEY).append(SPACER);
		}
		if (autoIncrement) {
			sb.append(AUTOINCREMENT).append(SPACER);
		}
		sb.append(fieldType);
		
		return sb.toString();
	}
}